import urllib.request as ur
import urllib.parse as up
import json

while True:
    word = input("请输入内容：")
    data = {
        'kw': word,
    }
    #####对data进行编码（字符串）
    data_url = up.urlencode(data)

    ####### 构造request对象，添加访问的url接口与请求数据
    requst = ur.Request(
        url='https://fanyi.baidu.com/sug',
        data=data_url.encode('utf-8'),  ##对编码后的数据转译成字节
    )

    response = ur.urlopen(requst).read()  # 得到返回的response对象
    ret = json.loads(response)  ###将json对象转化成Python对象
    translate = ret['data'][0]['v']

    print("翻译结果:", translate)
